package com.itheima.health.service.impl;

import com.itheima.health.common.MessageConst;
import com.itheima.health.dao.OrderSettingDao;
import com.itheima.health.entity.Result;
import com.itheima.health.exception.BusinessRuntimeException;
import com.itheima.health.pojo.Member;
import com.itheima.health.pojo.Order;
import com.itheima.health.pojo.OrderSetting;
import com.itheima.health.service.OrderSettingService;
import com.itheima.health.vo.AdvanceOrderDTO;
import com.itheima.health.vo.AdvanceOrderVO;
import com.itheima.health.vo.DataVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.xml.crypto.Data;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.*;

/**
 * @Author 山外小王
 * @Description
 * @Date 2024/04/17/15:49
 */
@Service
public class OrderSettingServiceImpl implements OrderSettingService {
    @Autowired
    private OrderSettingDao orderSettingDao;
    @Override
    public void add(List<OrderSetting> orderSettingList) {
        if (orderSettingList != null && orderSettingList.size()>0) {
            for (OrderSetting orderSetting : orderSettingList) {
                //判断下此日期是否存在
                Date orderDate = orderSetting.getOrderDate();
                long count = orderSettingDao.findCountByOrdrDate(orderDate);
                if (count > 0) {
                    //存在执行更新操作 //根据当前日期修改 可预约人数
                    orderSettingDao.update(orderSetting);
                }else {
                    //不存在执行添加操作 添加日期和可预约人数
                    orderSettingDao.add(orderSetting);
                }
            }

        }
    }

    @Override
    public List<DataVO> getOrderSettingByMonth(String year, String month) {
        //当前月的第一天
        LocalDate firstDay = LocalDate.of(Integer.parseInt(year),Integer.parseInt(month),1);
        Date dateFirst = java.sql.Date.valueOf(firstDay);
        //当前月的最后一天
        LocalDate lastDay = LocalDate.of(Integer.parseInt(year),Integer.parseInt(month),firstDay.lengthOfMonth());
        Date dateLast = java.sql.Date.valueOf(lastDay);
        List<OrderSetting> orderSettingList = orderSettingDao.getOrderSettingByMonth(dateFirst,dateLast);
        List<DataVO> dataVOList = new ArrayList<>();
        for (OrderSetting orderSetting : orderSettingList) {
            DataVO dataVO = new DataVO();
            dataVO.setDate(orderSetting.getOrderDate().getDate());
            dataVO.setNumber(orderSetting.getNumber());
            dataVO.setReservations(orderSetting.getReservations());
            dataVOList.add(dataVO);
        }
        return dataVOList;
    }

    @Override
    public void editNumberByDate(OrderSetting orderSetting) {
        //获取今天的日期
        LocalDate today = LocalDate.now();
        Date dateNow = java.sql.Date.valueOf(today);
        //获取当前月的最后一天
        LocalDate lastDay = today.withDayOfMonth(today.lengthOfMonth());
        Date dateLast = java.sql.Date.valueOf(lastDay);
        //获取点击日期
        Date currentDate = orderSetting.getOrderDate();
        if (currentDate.equals(dateNow) || currentDate.after(dateNow) &&
                currentDate.equals(dateLast) || currentDate.before(dateLast)){
            orderSettingDao.update(orderSetting);
        }
    }

    @Override
    public Result submit(AdvanceOrderDTO orderDTO) throws ParseException {
        Order order = new Order();
        String orderDate = orderDTO.getOrderDate();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date parse = sdf.parse(orderDate);
        //判断是不是会员
        int count = orderSettingDao.countTelephone(orderDTO.getTelephone());

        if (count == 0) {
            Member member = new Member();
            member.setRegTime(parse);
            member.setName(orderDTO.getName());
            member.setPhoneNumber(orderDTO.getTelephone());
            member.setIdCard(orderDTO.getIdCard());
            member.setSex(orderDTO.getSex());
            //注册成为会员
            orderSettingDao.registerMember(member);
        }
        //获取会员id
        int memberId = orderSettingDao.getmemberId(orderDTO.getTelephone());
        //存入预约信息
        //一个会员同一天同一个套餐不可重复预约
        //查找一个会员一个日期的所有预约套餐 如果套餐相同则不可预约
        Integer[] setmealIds = orderSettingDao.selectSetmealIds(memberId,parse);
        for (Integer setmealId : setmealIds) {
            if (setmealId.equals(Integer.parseInt(orderDTO.getSetmealId()))){
                throw new BusinessRuntimeException("一个会员同一天同一个套餐不可重复预约");
            }
        }
        //如果已预约数量>可预约数量则不可预约
        //根据日期查找预约人数和已预约人数
        int number = orderSettingDao.selectNumber(parse);
        int reservations = orderSettingDao.selectReservations(parse);
        if (reservations >= number) {
            throw new BusinessRuntimeException("预约人数已满");
        }
        //预约成功 存入数据库
        order.setMemberId(memberId);
        order.setOrderDate(parse);
        order.setOrderType(Order.ORDERTYPE_WEIXIN);
        order.setOrderStatus(Order.ORDERSTATUS_NO);
        Integer setmealId = Integer.valueOf(orderDTO.getSetmealId());
        order.setSetmealId(setmealId);
        orderSettingDao.addOrder(order);
        //预约成功 在当天已预约数量+1
        //当天预约人数+1
        reservations = reservations +1;
        OrderSetting orderSetting = new OrderSetting();
        orderSetting.setReservations(reservations);
        orderSetting.setNumber(number);
        orderSetting.setOrderDate(parse);
        orderSettingDao.update(orderSetting);
        return new Result(true, MessageConst.ORDER_SUCCESS,order);
    }
}
